<template>
    <div class="subjectForm">
      <!-- element组件库的Dialog对话框默认可以通过点击 modal 关闭 Dialog，即点击空白处弹框可关闭。 -->
      <!-- :visible.sync 子组键改变父组件的值 控制子组件的显示-->
      <el-dialog title="新增题目" :visible.sync="addDialogIsShow" :before-close="handleClose" :close-on-click-modal="false">
      <el-form :model="subject" label-width="80px" :rules="addSubjectRules" ref="addSubjectForm">
        <el-form-item label="题目" prop="sjname">
          <el-col :span="16">
           <el-input v-model="subject.sjname" placeholder="请输入题目" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="分数" prop="sjscore">
          <el-col :span="16">
            <el-input v-model="subject.sjscore" placeholder="请设定分数" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="内容" prop="sjcontent">
          <el-col :span="16">
            <el-input type="textarea" :rows="5" v-model="subject.sjcontent" placeholder="请输入描述" />
          </el-col>
        </el-form-item>
        <el-form-item label="答案" prop="sjanswer">
          <el-col :span="16">
            <el-input type="textarea" :rows="5" v-model="subject.sjanswer" placeholder="请输入答案" />
          </el-col>
        </el-form-item>
      </el-form>
      <el-row>
         <el-col :span="12">
         <span>&nbsp;</span></el-col>
        <el-col :span="6">
           <span><el-button type="primary" @click="addSubject('addSubjectForm')">添加</el-button></span>
        </el-col>

    </el-row>
      </el-dialog>
    </div>
</template>

<script>
  import api from "../../../../axios/api"
  import rules from "../../../../static/js/validator/rules.js"
    var initfavOption = [];
    export default {
        name: "addSubject",
        data(){
          return {subject:{},
              checkAll: false,
              isIndeterminate: true,
              addSubjectRules:{sjname:[{required:true,message:"题目不能为空",trigger:'blur'}],
                sjscore:[{required:true,message:"分数不能为空",trigger:'blur'},{trigger:'change',validator:rules.FormValidate.Form().isBtnOneToHundred}],
                sjcontent:[{required:true,message:"内容不能为空",trigger:'blur'}],
                sjanswer:[{required:true,message:"答案不能为空",trigger:'blur'}]},
              addDialogIsShow:false
          }
        },
      methods: {
        init() {
          this.addDialogIsShow = true;
        },
        handleCheckAllChange(val) {
          this.subject.favs = val ? initfavOption : [];
          this.isIndeterminate = false;
        },
        handleCheckedFavsChange(value) {
          let checkedCount = value.length;
          this.checkAll = checkedCount === this.favs.length;
          this.isIndeterminate = checkedCount > 0 && checkedCount < this.favs.length;
        },
        //关闭页面
        handleClose() {
          this.addDialogIsShow = false;
          //给父组件传参
          this.$emit("addDialogIsShow", this.addDialogIsShow);
        },
        addSubject:function(addSubjectForm){
          this.$refs[addSubjectForm].validate(valid => {//此处refs对象和标签上的ref对应
            if (valid) {
              //如果通过验证 to do...
              console.log(this.subject);
              alert(this.subject.sjname+"添加成功");
              this.setDataFromAxios();
              this.handleClose();
            } else {
              console.log('error submit!!')
              return false
            }
          });
        },
        //关闭页面
        handleClose() {
          this.addDialogIsShow = false;
          //给父组件传参
          this.$emit("addDialogIsShow", this.addDialogIsShow);
        },
        setDataFromAxios:function(){
         alert(this.$store.state.host);
          //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
          api.setDataFromAxios("/api/subjectController/addSubject",this.subject,"post")
            .then(res=>{this.leagues = res.leagues});//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
        },
        updateView(e) {
          this.$forceUpdate();
        }

      },
      //监听
      watch: {
        addDialogIsShow(newValue, oldValue) {//配合:visible.sync监听变化
          this.addDialogIsShow = newValue;
        }
      }
    }
</script>
<style scoped lang="stylus">
 .subjectForm
   margin 0 auto
   width 1000px
   text-align left
</style>
